import { Directive } from 'cydon'
import { prevent } from './index'

export const dragText = (name: string, value: string): Directive | void => {
	if (name == 'c-dragtext') {
		return {
			f(el: HTMLElement) {
				el.addEventListener('dragover', prevent)
				el.addEventListener('drop', async (e: DragEvent) => {
					prevent(e)
					let tags = e.dataTransfer!.getData('text/plain')
					if (!tags) {
						const file = e.dataTransfer!.files[0]
						if (file) {
							tags = await file.text()
						}
					}
					this[value] = tags
				})
			},
		}
	}
}